package edu.study.module.student.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ultrawise.survey.common.utils.DateUtils;
import com.ultrawise.survey.common.utils.RequestGetIp;
import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.ServiceException;

import edu.study.module.student.service.SubworklogService;
import edu.study.module.student.service.WorkInfoService;
import edu.study.module.student.vo.StudentInfo;
import edu.study.module.student.vo.Subworklog;
import edu.study.module.student.vo.WorkInfo;

@Controller
@RequestMapping("/subwork")
public class SubWorkControl extends BaseContoller {
	
	@Autowired
	WorkInfoService workinfoservice;
	
	@Autowired
	SubworklogService subworklogservice;

	@RequestMapping("/subindex")
	public String subwork(WorkInfo wkinfo,HttpServletRequest request){
		if(wkinfo!=null && wkinfo.getId()>0)
			try {
				wkinfo=(WorkInfo) workinfoservice.queryByID(wkinfo.getId(), wkinfo, WorkInfo.class);
			} catch (ServiceException e) {
				System.out.println("查询结果错误:"+wkinfo.getId()+"不存在。");
				wkinfo = null;
			}
		request.setAttribute("wkinfo", wkinfo);
		return "/subwork/index";
	}
	
	@RequestMapping("/uploadwork")
	public ModelAndView uploadwork(Subworklog sublog,HttpServletRequest request){
		
		StudentInfo sinfo = new StudentInfo();
		sinfo.setId("000001");//(StudentInfo)request.getSession().getAttribute("LOGINUSER");
		try {
			sublog.setSt_code(sinfo.getId());
			sublog.setSu_datetime(DateUtils.toString(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
			sublog.setSu_ip(RequestGetIp.getIpAddr(request));
			sublog = (Subworklog)subworklogservice.saveObj(sublog);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		request.setAttribute("message", "您已经成功提交作业，请返回。");
		return new ModelAndView("/massage");
	}
}
